package com.yyb.common.utils;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * @Author yuyb40219
 * @Date 2022/1/19 15:15
 * Description: 供外部调用的公共http调用类
 */
public class HttpCommonUtils {

    private static final CloseableHttpClient HTTP_CLIENT = HttpUtil.newHttpClient();

    /* ------------------------- GET方法 ------------------------- */
    /**
     * get方法
     * @return
     * @throws Exception
     */
    public static String doGet(String url, Map<String, Object> head, String logTrace) throws Exception {
        return HttpUtil.doRequest(HTTP_CLIENT, HttpGet.METHOD_NAME, url, head, "", logTrace, StandardCharsets.UTF_8, StandardCharsets.UTF_8, 15000);
    }

    /**
     * get方法-自定义入参、出参的字符集编解码格式
     * @return
     * @throws Exception
     */
    public static String doGetCharset(String url, Map<String, Object> head, Charset reqCharset, Charset respCharset, String logTrace) throws Exception {
        return HttpUtil.doRequest(HTTP_CLIENT, HttpGet.METHOD_NAME, url, head, "", logTrace, reqCharset, respCharset, 15000);
    }

    /**
     * get方法-自定义超时时长
     * @return
     * @throws Exception
     */
    public static String doGetTimedOut(String url, Map<String, String> head, int timeout, String logTrace) throws Exception {
        return HttpUtil.getTimeout(HTTP_CLIENT, url, head, timeout);
    }

    /* ------------------------- POST方法 ------------------------- */
    /**
     * post方法
     * @return
     * @throws Exception
     */
    public static String doPost(String url, Map<String, Object> head, String body, String logTrace) throws Exception {
        return HttpUtil.doRequest(HTTP_CLIENT, HttpPost.METHOD_NAME, url, head, body, logTrace, StandardCharsets.UTF_8, StandardCharsets.UTF_8, 15000);
    }

    /**
     * post方法-自定义入参、出参的字符集编解码格式
     * @return
     * @throws Exception
     */
    public static String doPostCharset(String url, Map<String, Object> head, String body, Charset reqCharset, Charset respCharset, String logTrace) throws Exception {
        return HttpUtil.doRequest(HTTP_CLIENT, HttpPost.METHOD_NAME, url, head, body, logTrace, reqCharset, respCharset, 15000);
    }

    /**
     * post方法-自定义超时时长
     * @return
     * @throws Exception
     */
    public static String doPostTimedOut(String url, Map<String, Object> head, String body, String logTrace, int timeout) throws Exception {
        return HttpUtil.doRequest(HTTP_CLIENT, HttpPost.METHOD_NAME, url, head, body, logTrace, StandardCharsets.UTF_8, StandardCharsets.UTF_8, timeout);
    }

    /**
     * post方法-自定义超时时长 and 入参、出参的字符集编解码格式
     * @return
     * @throws Exception
     */
    public static String doPostTimedOutAndCharset(String url, Map<String, Object> head, String body, String logTrace, Charset reqCharset, Charset respCharset, int timeout) throws Exception {
        return HttpUtil.doRequest(HTTP_CLIENT, HttpPost.METHOD_NAME, url, head, body, logTrace, reqCharset, respCharset, timeout);
    }

    /* ------------------------- PUT方法 ------------------------- */

    /* ------------------------- DELETE方法 ------------------------- */

    /* ------------------------- 自定义使用方法 ------------------------- */
    public static String doRequest(String method, String url,
                                   final Map<String, Object> head, String body, String logTrace,
                                   Charset reqCharset, Charset respCharset, int timeout) throws Exception {
        return HttpUtil.doRequest(HTTP_CLIENT, method, url, head, body, logTrace, reqCharset, respCharset, timeout);
    }
}